(function () {
    validateRule();
    getLogName();

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                name: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: icon + "请输入姓名"
                }
            },
            submitHandler: function () {
                var data = buildData();
                console.log(data);
                if (!data.products || data.products.length == 0) {
                    layer.alert("请选择产品");
                    return false
                }
                $.ajax({
                    url: getProjectName()+"/invoice/add",
                    type: "POST",
                    contentType: "application/json",
                    dataType: "JSON",
                    data: JSON.stringify(data),// 你的formid
                    success: function (data) {
                        if (data.code == 0) {
                            parent.layer.msg("操作成功");
                            parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                            parent.layer.close(index);
                        } else {
                            parent.layer.alert(data.msg)
                        }

                    },
                    error: function (request) {
                        parent.layer.alert("Connection error");
                    }
                });
                return false;
            }
        })
    }


    function buildData() {
        var data = {};
        data.agentId = $("#signupForm input[name=agentId]").val();
        data.linkman = $("#signupForm input[name=linkman]").val();
        data.tel = $("#signupForm input[name=tel]").val();
        data.deliverAddress = $("#signupForm input[name=deliverAddress]").val();
        data.deliverDate = Date.parse($("#signupForm input[name=deliverDate]").val());
        data.remark = $("#signupForm input[name=remark]").val();
        data.code = $("#signupForm input[name=code]").val();
        data.logistics = $("#signupForm select[name=logistics] option:selected").val();
        data.products = buildProducts();

        return data;
    }

    function buildProducts() {
        var products = [];
        $("#signupForm .product-list div[class =product-group]").each(function () {
            var product = {};
            product.productId = $(this).children("input[name=number]").attr("id");
            product.productSize = $(this).children("input[name=number]").val();
            product.code = $(this).children("input[name=code]").val();
            products.push(product);
        });
        return products;
    }


    var productIndex, agentIndex;
    $(document).on("click", "a,i", function () {
        switch ($(this).attr("option")) {
            case "agentSelect": {
                agentSelect();
                break;
            }
            case "productSelect": {
                productSelect();
                break;
            }
            case "productDelete": {
                $(this).parents("div[class=product-group]").remove();
            }
        }
    });

    /**
     *
     */
    function productSelect() {
        productIndex = layer.open({
            type: 2,
            title: '选择产品',
            maxmin: false,
            shadeClose: false, // 点击遮罩关闭层
            area: ['100%', '100%'],
            content: getProjectName()+'/invoice/view/product/select' // iframe的url
        });
    }

    /**
     *
     */
    function agentSelect() {
        agentIndex = layer.open({
            type: 2,
            title: '选择经销商',
            maxmin: false,
            shadeClose: false, // 点击遮罩关闭层
            area: ['100%', '100%'],
            content: getProjectName()+'/invoice/view/agent/select' // iframe的url
        });
    }

    /**
     *
     * @param data
     */
    window.productCallback = function (data) {
        var hasExist = false;
        $(".product-list div[class=product-group]").each(function () {
            if ($(this).attr("id") === data.productId) {
                hasExist = true;
            }
        });

        if (hasExist) {
            layer.msg(data.productName + "已经选择");
            return;
        }

        var div = $('<div id="' + data.productId + '" class="product-group"></div>');
        $(div).append('<span>' + data.productName + '</span>');
        $(div).append('<input id="' + data.productId + '" class="form-control" name="number" type="" required placeholder="产品数量(必填)">');
        $(div).append('<input id="' + data.productId + '" class="form-control" name="code" type="" required placeholder="产品批号(必填)">');
        $(div).append('<i class="fa fa-remove" option="productDelete"></i>');
        $(".product-list").append(div);
        layer.close(productIndex);
    };

    /**
     *
     * @param data
     */
    window.agentCallback = function (data) {
        $("#agentId").val(data.productId);
        $("#agentName").val(data.agentName);
        $("#tel").val(data.tel);
        $("#linkman").val(data.linkman);
        $("#deliverAddress").val(data.address);
        layer.close(agentIndex);
    };
})();

